<template>
  <div class="page-container">
    <div class="page-content">
      <div class="panel-container">
        <div class="panel-content">
          <div class="panel-line space-between">
            <div class="panel-left">
              <el-radio-group v-model="tabActive" style="margin-right: 30px" size="mini">
                <el-radio-button v-for="item in tabList" :key="item.value" :label="item.value">{{ item.label }}({{ item.num }})</el-radio-button>
              </el-radio-group>
              <el-button class="show-search" type="text" @click="handleShowSearch">
                <span v-if="showSearch"> 收起搜索 </span>
                <span v-else> 展开搜索 </span>
                <i class="el-icon-arrow-down" :class="{ rotate: showSearch }"></i>
              </el-button>
            </div>
            <div class="panel-right">
              <el-button type="primary" @click="handlePublish" size="mini">+ 新增</el-button>
            </div>
          </div>
          <!--搜索-->
          <SearchUiNew ref="SearchUiNew" style="margin-top: 10px" v-show="showSearch" :searchFormArr="searchFormArr" @searchParma="searchParma"></SearchUiNew>
        </div>
      </div>
      <!--数据信息-->
      <div class="panel-container no-margin">
        <el-table stripe :data="dataList" size="mini" :header-cell-style="{ background: '#F5F5F5', color: '#000000' }" ref="table" style="width: 100%" @selection-change="handleSelectionChange" v-loading="dataListLoading">
          <af-table-column type="selection" fixed="left" width="55"> </af-table-column>
          <af-table-column align="center" fixed="left" header-align="center" show-overflow-tooltip label="拍卖名称" prop="title" width="200"></af-table-column>
          <af-table-column align="center" header-align="center" show-overflow-tooltip label="发布人" prop="pub_man_name"></af-table-column>
          <af-table-column align="center" header-align="center" show-overflow-tooltip label="公告发布时间" prop="created_time" width="150"></af-table-column>
          <af-table-column align="center" header-align="center" show-overflow-tooltip label="浏览量" prop="view_count"></af-table-column>
          <af-table-column align="center" fixed="right" header-align="center" show-overflow-tooltip label="操作" width="150">
            <template slot-scope="scope">
              <el-button type="text" @click="handleEdit(scope)">编辑</el-button>
              <el-button type="text" @click="handleDetail(scope)">查看</el-button>
              <el-button type="text" @click="handleDelete(scope)" class="button-danger" v-if="scope.row.power_sts !== 4">删除</el-button>
            </template>
          </af-table-column>
        </el-table>
        <el-pagination
          small
          background
          :current-page="pageNumber"
          :page-size="pageSize"
          :page-sizes="[10, 20, 50, 100]"
          :total="pageTotal"
          @current-change="currentChangeHandle"
          @size-change="sizeChangeHandle"
          layout="total, sizes, prev, pager, next, jumper"
          style="margin-top: 20px; text-align: right"
        ></el-pagination>
        <div class="panel-line" style="margin-top: 20px">
          <el-button v-if="tabActive !== 4" type="danger" @click="handleDeleteMultiple" size="mini" :disabled="multipleSelection.length === 0">批量删除</el-button>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import { apiInnerList, apiInnerDel } from '@/api/biddingNotice'

export default {
  name: 'InnerNoticeManagement',
  components: {},
  data() {
    return {
      query: {},
      multipleSelection: [],
      showSearch: false,
      tabActive: '',
      tabList: [{ value: '', label: '全部', num_name: 'total', num: 0 }],
      searchFormArr: [
        {
          fieldName: '拍卖名称:',
          type: 'input',
          field: 'title',
          placeholder: '请输入拍卖名称',
        },
        {
          fieldName: '发布人:',
          type: 'input',
          field: 'pub_man_name',
          placeholder: '请输入发布人',
        },
        {
          fieldName: '关键字精准搜索:',
          type: 'radioGroup',
          field: 'is_precise',
          default: 0,
          option: [
            { value: 1, label: '是' },
            { value: 0, label: '否' },
          ],
        },
        {
          fieldName: '所属地区:',
          type: 'cascader',
          field: 'zone',
          props: { value: 'code', label: 'name', checkStrictly: true },
          option: [],
        },
        {
          fieldName: '发布时间:',
          type: 'timePicker',
          field: 'time',
          placeholder: '请选择发布时间',
          timeType: 'daterange',
          format: 'yyyy-MM-dd',
        },
      ],
      searchParams: {
        title: '',
        pub_man_name: '',
        is_precise: '',
        sheng_id: '',
        shi_id: '',
        start_time: '',
        end_time: '',
      },
      dataList: [],
      pageNumber: 1,
      pageSize: 10,
      pageTotal: 0,
      dataListLoading: false,
    }
  },
  activated() {
    this.$refs.table.doLayout()
  },
  mounted() {
    this.$bus.$on('innerNoticeListUpdate', this.getDataList)
    this.init()
  },
  destroyed() {
    this.$bus.$off('innerNoticeListUpdate', this.getDataList)
  },
  methods: {
    // 初始化
    init() {
      this.getSelectList()
      this.query = this.$route.query
      const { start_time, end_time, tab } = this.query
      tab && (this.tabActive = tab)

      if (start_time && end_time) {
        this.showSearch = true
        this.searchParams.start_time = start_time
        this.searchParams.end_time = end_time
      }

      this.$nextTick(() => {
        const setItemList = []
        if (start_time && end_time) {
          setItemList.push({ key: 'time', value: [start_time, end_time] })
        }

        if (setItemList.length > 0) {
          this.$refs.SearchUiNew.setItemValue(setItemList)
        } else {
          this.getDataList()
        }
      })
    },
    handleShowSearch() {
      this.showSearch = !this.showSearch
    },
    // 获取下拉数据
    getSelectList() {
      this.$store.dispatch('select/getCityList').then((res) => {
        const formItem = this.searchFormArr.find((item) => item.field === 'zone')
        formItem.option = res
      })
    },
    // 获取数据列表
    getDataList() {
      this.dataListLoading = true
      let info = { pageNumber: this.pageNumber, pageSize: this.pageSize }
      info = Object.assign(info, this.searchParams)
      apiInnerList(info)
        .then((res) => {
          const { data, total } = res
          this.dataList = data
          this.pageTotal = total
          this.dataListLoading = false
          this.tabList[0].num = total
          this.$refs.table.doLayout()
        })
        .catch((e) => {
          this.dataList = []
          this.pageTotal = 0
          this.dataListLoading = false
        })
    },
    // 列表查询
    searchParma(e) {
      let list = ['zone', 'time']
      this.searchFormArr.forEach((item) => {
        if (list.includes(item.field)) return
        if (e[item.field]) {
          this.searchParams[item.field] = e[item.field]
        } else {
          this.searchParams[item.field] = ''
        }
      })
      if (e.zone) {
        this.searchParams.sheng_id = e.zone[0] || ''
        this.searchParams.shi_id = e.zone[1] || ''
        this.searchParams.zone_id = e.zone[2] || ''
      } else {
        this.searchParams.sheng_id = this.searchParams.shi_id = this.searchParams.zone_id = ''
      }
      if (e.time) {
        this.searchParams.start_time = e.time[0]
        this.searchParams.end_time = e.time[1]
      } else {
        this.searchParams.start_time = this.searchParams.end_time = ''
      }
      this.pageNumber = 1
      this.getDataList()
    },
    // 每页数
    sizeChangeHandle(val) {
      this.pageSize = val
      this.pageNumber = 1
      this.getDataList()
    },
    // 当前页
    currentChangeHandle(val) {
      this.pageNumber = val
      this.getDataList()
    },
    // 查看
    handleDetail(scope) {
      this.$store.dispatch('permission/routeAuthorization', 'InnerNoticeDetail').then((res) => {
        let info = { pageNumber: this.pageNumber, pageSize: this.pageSize, power_sts: this.tabActive }
        info = Object.assign(info, this.searchParams)
        info.position = scope.$index
        window.localStorage.setItem('inner-notice-edit-table-query', JSON.stringify(info))
        this.$bus.$emit('forceUpdate', 'InnerNoticeDetail')
        this.$router.push({
          path: '/biddingNotice/innerNoticeDetail',
          query: {
            id: scope.row.id,
          },
        })
      })
    },
    // 编辑
    handleEdit(scope) {
      this.$store.dispatch('permission/routeAuthorization', 'InnerNoticePublish').then((res) => {
        let info = { pageNumber: this.pageNumber, pageSize: this.pageSize, power_sts: this.tabActive }
        info = Object.assign(info, this.searchParams)
        info.position = scope.$index
        window.localStorage.setItem('inner-notice-edit-table-query', JSON.stringify(info))
        // 编辑新窗口打开
        window.open(`${location.origin}/#/biddingNotice/innerNoticePublish?id=${scope.row.id}`)
      })
    },
    // 发布
    handlePublish() {
      this.$store.dispatch('permission/routeAuthorization', 'InnerNoticePublish').then((res) => {
        this.$bus.$emit('forceUpdate', 'InnerNoticePublish')
        this.$router.push({
          path: '/biddingNotice/innerNoticePublish',
        })
      })
    },
    // 删除
    handleDelete(scope) {
      this.innerDelete([scope.row.id])
    },
    // 表格多选
    handleSelectionChange(val) {
      this.multipleSelection = val
    },
    // 多选删除
    handleDeleteMultiple() {
      this.innerDelete(this.multipleSelection.map((item) => item.id))
    },
    // 删除
    innerDelete(ids) {
      window.enterPreventDefault = false
      this.$confirm('确定要删除选中信息？', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning',
      })
        .then(() => {
          window.enterPreventDefault = true

          this.dataListLoading = true
          apiInnerDel({ ids })
            .then(() => {
              this.dataListLoading = false
              this.$message({
                type: 'success',
                message: '删除成功!',
              })
              if (ids.length === this.dataList.length && this.pageNumber > 1) {
                this.pageNumber--
              }
              this.getDataList()
            })
            .catch(() => {
              this.dataListLoading = false
            })
        })
        .catch(() => {
          window.enterPreventDefault = true
        })
    },
  },
}
</script>
